<?php
$result = FSM::run('Cms.widgetDefinition.get', array('id'=>$widgetId));
$widgetName = '';
if (!$result->hasErrors())
    $widgetName = $result->model->name;
$this->breadcrumbs=array(
    'Widgets'=>array('/Cms/admin/widget/admin'),
	$widgetName=>array('/Cms/admin/widgetData','WidgetData[widget_id]' => $widgetId),
	'Pages use '.$widgetName,
);

$this->menu=array(
    array('label'=>'Activate selected items', 'url'=>array('/Core/service/ajax', "SID"=>"Cms.pageWidget.changeStatus", 'value'=>1), 'linkOptions'=>array('class'=>'change-status')),
	array('label'=>'Deactivate selected items', 'url'=>array('/Core/service/ajax', "SID"=>"Cms.pageWidget.changeStatus", 'value'=>0), 'linkOptions'=>array('class'=>'change-status')),
);

$script = "
jQuery('#page-widget-grid .status-column a').live('click',function() {
    jQuery.fn.yiiGridView.update('page-widget-grid', {
        url:jQuery(this).attr('href'),
        success:function() {
            jQuery.fn.yiiGridView.update('page-widget-grid');
        }
    });
    return false;
});
jQuery('.crud-menu .change-status').live('click',function() {
    var data = [];
    jQuery.each(jQuery('input.select-on-check'), function(){
        if (jQuery(this).attr('checked'))
            data.push(jQuery(this).val());
    });
    if (data.length <= 0)
    {
        alert('No item selected.');
        return false;
    }
    
    jQuery.fn.yiiGridView.update('page-widget-grid', {
        url:jQuery(this).attr('href'),
        data : {'ids' : data},
        success:function() {
            jQuery.fn.yiiGridView.update('page-widget-grid');
        }
    });
    return false;
});
jQuery.fn.init_workflow_filter = function()
{
    var workflow_input = jQuery('<input />').attr({
        'id' : 'PageWidget_workflowId',
        'type' : 'hidden',
        'name' : 'PageWidget[workflowId]',
        'value' : jQuery('select#workflow').val()
    });
    jQuery('#page-widget-grid .filters td:first').append(workflow_input);
}
jQuery.fn.init_workflow_filter();
jQuery('select#workflow').change(function(){
    var value = parseInt(jQuery(this).val());
    if (value)
        jQuery('#PageWidget_workflowId').val(jQuery(this).val());
    else
        jQuery('#PageWidget_workflowId').val('');
    jQuery('#PageWidget_workflowId').trigger('change');
});
";
Yii::app()->clientScript->registerScript(__CLASS__.'#ChangeStatus', $script, CClientScript::POS_READY);

$script = "
jQuery('#content').delegate('.button-column a.view', 'click', function(){
    var url = jQuery(this).attr('href');
    jQuery.fancybox({
        'titleShow'         : false,
        'height'            : '100%',
        'width'             : '100%',
        'autoScale'         : true,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'type'              : 'iframe',
        'href'              : url,
        'onComplete'         : function(){
        }
    });
    return false;
});
";
$cs = Yii::app()->clientScript;
$cs->registerScript(__CLASS__.'#Preview', $script, CClientScript::POS_READY);
$cs->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.pack.js');
$cs->registerCssFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.css');
?>

<h2>Pages use <?php echo $widgetName;?></h2>

<label for="workflow">Workflow :</label>
<?php echo CHtml::dropDownList('workflow', $workflowSelected, $workflows, array('prompt'=>'---Show All---'));?>

<?php $grid = $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'page-widget-grid',
	'dataProvider'=>$model->searchByWidget($siteId, $widgetId),
	'filter'=>$model,
    'selectableRows'=>2,
    'selectionChanged'=>"updateSelectors",
    'afterAjaxUpdate'=>"js:function(){jQuery.fn.init_workflow_filter();}",
	'columns'=>array(
        array(
            'class'=>'CCheckBoxColumn',
            'value'=>'$data->id',
            'htmlOptions'=>array('width'=>'3%'),
        ), 
        array(
            'name'=>'page_id',
            'type'=>'raw',
            'value'=>'CHtml::link($data->page->title, array("/Cms/admin/page/update", "id"=>$data->page_id))." rev#".$data->revision_id',
        ),
        array(
            'header' => 'Default Url',
            'name' => 'pageUrl',
            'type' => 'raw',
            'value' => 'is_object($data->page) && is_object($data->page->defaultUrl) ? $data->page->defaultUrl->url : ""',
        ),
        array(
            'name' => 'widget_layout_id',
            'type'=>'html',
            'value' => 'CHtml::link($data->widgetLayout->name, array("/Cms/admin/widgetLayout/update", "id"=>$data->widget_layout_id))',
            'htmlOptions'=>array(
                'align'=>'center',
            )
        ),
        array(
            'name' => 'container',
            'value' => '$data->container',
            'htmlOptions'=>array(
                'align'=>'center',
            )
        ),
        array(
            'name' => 'ordering',
            'value' => '(int) $data->ordering',
            'filter'=>false,
            'sortable'=>false,
            'htmlOptions'=>array(
                'width'=>50,
                'align'=>'center',
            ),
            'visible'=>false,
        ),
        array(
            'name'=>'status',
            'type'=>'raw',
            'value'=>'CHtml::link($data->status, array("/Core/service/ajax", "SID"=>"Cms.pageWidget.changeStatus", "ids"=>$data->id, "value"=>$data->status ? 0 : 1), array("class"=>($data->status ? "active" : "")))',
            'filter'=>false,
            'sortable'=>false,
            'htmlOptions'=>array(
                'class'=>'status-column'
            )
        ),
		array(
			'class'=>'CButtonColumn',
            'template'=>'{view} {delete}',
            'viewButtonUrl'=>'is_object($data->page) && is_object($data->page->defaultUrl) ? "http://".trim(PageRevision::model()->getInitialWorkflowUrl($data->page->site_id), "/")."/".trim($data->page->defaultUrl->url, "/")."/?preview=1&page_widget_id=".$data->id."&revision=".$data->revision_id : "#"',
            'afterDelete'=>'function(th,status,data){if (status && data.length) alert(data);}',
		),
	),
));
 

if ($grid->dataProvider->ItemCount) {
    $this->menu[] = array('label' => 'Delete selected items', 'url'=>$this->createUrl('delete'), 'linkOptions' => array('onclick'=>'return multipleDelete("page-widget-grid",this.href)'));
}
Yii::app()->clientScript->registerScriptFile(Yii::app()->core->AssetUrl.'/scripts/gridview.js', CClientScript::POS_BEGIN);
?>